package com.erp.programmer.controller;


import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dto.*;
import com.erp.programmer.service.IncomingInspectionOrderService;
import com.erp.programmer.utils.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czh
 * @since 2021-05-24
 */
@RestController
@CrossOrigin
@RequestMapping("/incoming_inspection_order")
public class IncomingInspectionOrderController {

    @Autowired
    private IncomingInspectionOrderService incomingInspectionOrderService;

    @PostMapping("/save")
    public ResponseDto<PageDto<IncomingInspectionOrderDto>> save(@RequestBody IncomingInspectionOrderDto incomingInspectionOrderDto){

        if(incomingInspectionOrderDto == null){
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }

        CodeMsg validate = ValidateEntityUtil.validate(incomingInspectionOrderDto);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDto.errorByMsg(validate);
        }

        return incomingInspectionOrderService.add(incomingInspectionOrderDto);
    }

    @PostMapping("/delete")
    public ResponseDto<PageDto<IncomingInspectionOrderDto>> delete(@RequestBody IncomingInspectionOrderDto incomingInspectionOrderDto){

        if(incomingInspectionOrderDto == null){
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }

        CodeMsg validate = ValidateEntityUtil.validate(incomingInspectionOrderDto);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDto.errorByMsg(validate);
        }


        return incomingInspectionOrderService.delete(incomingInspectionOrderDto);
    }

    @PostMapping("/get_data")
    public ResponseDto<PageDto<IncomingInspectionOrderDto>> getData(@RequestBody PageDto<IncomingInspectionOrderDto> pageDto){

        return incomingInspectionOrderService.getData(pageDto);
    }

    @PostMapping("/edit_state")
    public ResponseDto<PageDto<IncomingInspectionOrderDto>> editState(@RequestBody EditStateDto editStateDto){

        return incomingInspectionOrderService.updateInspectState(editStateDto.getId(), Integer.valueOf(editStateDto.getState()));
    }

}

